<!DOCTYPE html>
<html lang="zh" dir="ltr" class="client-nojs">
<head>
<title>声明</title>
<meta charset="UTF-8">
<meta name="generator" content="MediaWiki 1.21.2">
<link rel="shortcut icon" href="../../../common/favicon.ico">
<link rel="stylesheet" href="../../../common/ext.css">
<meta name="ResourceLoaderDynamicStyles" content="">
<link rel="stylesheet" href="../../../common/site_modules.css">
<style>a:lang(ar),a:lang(ckb),a:lang(fa),a:lang(kk-arab),a:lang(mzn),a:lang(ps),a:lang(ur){text-decoration:none}#toc{display:none}.editsection{display:none}
/* cache key: mwiki1-mwiki_zh_:resourceloader:filter:minify-css:7:15cea3ec788a65b5187d4018eed543bf */</style>

<script src="../../../common/startup_scripts.js"></script>
<script>if(window.mw){
mw.config.set({"wgCanonicalNamespace":"","wgCanonicalSpecialPageName":false,"wgNamespaceNumber":0,"wgPageName":"c/language/declarations","wgTitle":"c/language/declarations","wgCurRevisionId":56726,"wgArticleId":7943,"wgIsArticle":true,"wgAction":"view","wgUserName":null,"wgUserGroups":["*"],"wgCategories":[],"wgBreakFrames":false,"wgPageContentLanguage":"zh","wgSeparatorTransformTable":["",""],"wgDigitTransformTable":["",""],"wgDefaultDateFormat":"zh","wgMonthNames":["","1月","2月","3月","4月","5月","6月","7月","8月","9月","10月","11月","12月"],"wgMonthNamesShort":["","1月","2月","3月","4月","5月","6月","7月","8月","9月","10月","11月","12月"],"wgRelevantPageName":"c/language/declarations","wgUserVariant":"zh","wgRestrictionEdit":[],"wgRestrictionMove":[]});
}</script><script>if(window.mw){
mw.loader.implement("user.options",function(){mw.user.options.set({"ccmeonemails":0,"cols":80,"date":"default","diffonly":0,"disablemail":0,"disablesuggest":0,"editfont":"default","editondblclick":0,"editsection":0,"editsectiononrightclick":0,"enotifminoredits":0,"enotifrevealaddr":0,"enotifusertalkpages":1,"enotifwatchlistpages":0,"extendwatchlist":0,"externaldiff":0,"externaleditor":0,"fancysig":0,"forceeditsummary":0,"gender":"unknown","hideminor":0,"hidepatrolled":0,"imagesize":2,"justify":0,"math":1,"minordefault":0,"newpageshidepatrolled":0,"nocache":0,"noconvertlink":0,"norollbackdiff":0,"numberheadings":0,"previewonfirst":0,"previewontop":1,"quickbar":5,"rcdays":7,"rclimit":50,"rememberpassword":0,"rows":25,"searchlimit":20,"showhiddencats":0,"showjumplinks":1,"shownumberswatching":1,"showtoc":0,"showtoolbar":1,"skin":"cppreference2","stubthreshold":0,"thumbsize":2,"underline":2,"uselivepreview":0,"usenewrc":0,"watchcreations":0,"watchdefault":0,"watchdeletion":0,
"watchlistdays":3,"watchlisthideanons":0,"watchlisthidebots":0,"watchlisthideliu":0,"watchlisthideminor":0,"watchlisthideown":0,"watchlisthidepatrolled":0,"watchmoves":0,"wllimit":250,"variant":"zh","language":"zh","searchNs0":true,"searchNs1":false,"searchNs2":false,"searchNs3":false,"searchNs4":false,"searchNs5":false,"searchNs6":false,"searchNs7":false,"searchNs8":false,"searchNs9":false,"searchNs10":false,"searchNs11":false,"searchNs12":false,"searchNs13":false,"searchNs14":false,"searchNs15":false,"gadget-MathJax":1,"gadget-ColiruCompiler":1});;},{},{});mw.loader.implement("user.tokens",function(){mw.user.tokens.set({"editToken":"+\\","patrolToken":false,"watchToken":false});;},{},{});
/* cache key: mwiki1-mwiki_zh_:resourceloader:filter:minify-js:7:258d7cd6aa9aa67dee25e01fb6a9e505 */
}</script>
<script>if(window.mw){
mw.loader.load(["mediawiki.page.startup","mediawiki.legacy.wikibits","mediawiki.legacy.ajax"]);
}</script>
<style type="text/css">/*<![CDATA[*/
.source-c {line-height: normal;}
.source-c li, .source-c pre {
	line-height: normal; border: 0px none white;
}
/**
 * GeSHi Dynamically Generated Stylesheet
 * --------------------------------------
 * Dynamically generated stylesheet for c
 * CSS class: source-c, CSS id: 
 * GeSHi (C) 2004 - 2007 Nigel McNie, 2007 - 2008 Benny Baumann
 * (http://qbnz.com/highlighter/ and http://geshi.org/)
 * --------------------------------------
 */
.c.source-c .de1, .c.source-c .de2 {font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;}
.c.source-c  {font-family:monospace;}
.c.source-c .imp {font-weight: bold; color: red;}
.c.source-c li, .c.source-c .li1 {font-weight: normal; vertical-align:top;}
.c.source-c .ln {width:1px;text-align:right;margin:0;padding:0 2px;vertical-align:top;}
.c.source-c .li2 {font-weight: bold; vertical-align:top;}
.c.source-c .kw1 {color: #0000dd;}
.c.source-c .kw2 {color: #0000ff;}
.c.source-c .kw3 {color: #0000dd;}
.c.source-c .kw4 {color: #0000ff;}
.c.source-c .co1 {color: #909090;}
.c.source-c .co2 {color: #339900;}
.c.source-c .coMULTI {color: #ff0000; font-style: italic;}
.c.source-c .es0 {color: #008000; font-weight: bold;}
.c.source-c .es1 {color: #008000; font-weight: bold;}
.c.source-c .es2 {color: #008000; font-weight: bold;}
.c.source-c .es3 {color: #008000; font-weight: bold;}
.c.source-c .es4 {color: #008000; font-weight: bold;}
.c.source-c .es5 {color: #008000; font-weight: bold;}
.c.source-c .br0 {color: #008000;}
.c.source-c .sy0 {color: #008000;}
.c.source-c .sy1 {color: #000080;}
.c.source-c .sy2 {color: #000040;}
.c.source-c .sy3 {color: #000040;}
.c.source-c .sy4 {color: #008080;}
.c.source-c .st0 {color: #008000;}
.c.source-c .nu0 {color: #000080;}
.c.source-c .nu6 {color:#000080;}
.c.source-c .nu8 {color:#000080;}
.c.source-c .nu12 {color:#000080;}
.c.source-c .nu16 {color:#000080;}
.c.source-c .nu17 {color:#000080;}
.c.source-c .nu18 {color:#000080;}
.c.source-c .nu19 {color:#000080;}
.c.source-c .ln-xtra, .c.source-c li.ln-xtra, .c.source-c div.ln-xtra {background-color: #ffc;}
.c.source-c span.xtra { display:block; }

/*]]>*/
</style><!--[if lt IE 7]><style type="text/css">body{behavior:url("/mwiki/skins/cppreference2/csshover.min.htc")}</style><![endif]--></head>
<body class="mediawiki ltr sitedir-ltr ns-0 ns-subject page-c_language_declarations skin-cppreference2 action-view cpp-navbar">
        <!-- header -->
        <!-- /header -->
        <!-- content -->
<div id="cpp-content-base">
            <div id="content">
                <a id="top"></a>
                <div id="mw-js-message" style="display:none;"></div>
                                <!-- firstHeading -->
<h1 id="firstHeading" class="firstHeading">声明</h1>
                <!-- /firstHeading -->
                <!-- bodyContent -->
                <div id="bodyContent">
                                        <!-- tagline -->
                    <div id="siteSub">来自cppreference.com</div>
                    <!-- /tagline -->
                                        <!-- subtitle -->
                    <div id="contentSub"><span class="subpages">&lt; <a href="../../c.html" title="c">c</a>‎ | <a href="../language.html" title="c/language">language</a></span></div>
                    <!-- /subtitle -->
                                                            <!-- bodycontent -->
                    <div id="mw-content-text" lang="zh" dir="ltr" class="mw-content-ltr"><div class="t-navbar" style=""><div class="t-navbar-sep"> </div><div class="t-navbar-head"><a href="../../c.html" title="c"> C</a><div class="t-navbar-menu"><div><div><table class="t-nv-begin" cellpadding="0" style="line-height:1.1em;">
<tr class="t-nv"><td colspan="5"> <a href="../language.html" title="c/language">语言</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../header.html" title="c/header">头文件</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../types.html" title="c/types">类型支持</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../program.html" title="c/program">程序工具</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../variadic.html" title="c/variadic">变参数函数支持</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../memory.html" title="c/memory">动态内存管理</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../error.html" title="c/error">错误处理</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../chrono.html" title="c/chrono">日期和时间工具</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../string.html" title="c/string">字符串库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../algorithm.html" title="c/algorithm">算法</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../numeric.html" title="c/numeric">数值</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../io.html" title="c/io">输入/输出支持</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../locale.html" title="c/locale">本地化支持</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../thread.html" title="c/thread">线程支持</a> <span class="t-mark-rev t-since-c11">(C11)</span> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../atomic.html" title="c/atomic">原子操作</a> <span class="t-mark-rev t-since-c11">(C11)</span> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../experimental.html" title="c/experimental">技术规范</a> </td></tr>
</table></div><div></div></div></div></div><div class="t-navbar-sep"> </div><div class="t-navbar-head"><a href="../language.html" title="c/language">C 语言</a><div class="t-navbar-menu"><div><div><table class="t-nv-begin" cellpadding="0" style="">
<tr class="t-nv-h2"><td colspan="5"> <a href="basic_concepts.html" title="c/language/basic concepts">基本概念</a></td></tr>
<tr class="t-nv-h2"><td colspan="5"> <a href="../keyword.html" title="c/keyword">关键词</a></td></tr>
<tr class="t-nv-h2"><td colspan="5"> <a href="../preprocessor.html" title="c/preprocessor">预处理器</a></td></tr>
<tr class="t-nv-h2"><td colspan="5"> <a href="statements.html" title="c/language/statements">语句</a></td></tr>
<tr class="t-nv-h2"><td colspan="5"> <a href="expressions.html" title="c/language/expressions">表达式</a></td></tr>
<tr class="t-nv-h2"><td colspan="5"> <a href="initialization.html" title="c/language/initialization">初始化</a></td></tr>
<tr class="t-nv-h2"><td colspan="5"> <strong class="selflink">声明</strong></td></tr>
<tr class="t-nv-h2"><td colspan="5"> <a href="functions.html" title="c/language/functions">函数</a></td></tr>
<tr class="t-nv"><td colspan="5"> 杂项</td></tr>
<tr class="t-nv-h2"><td colspan="5"> <a href="history.html" title="c/language/history">C 的历史</a></td></tr>
<tr class="t-nv-h2"><td colspan="5"> <a href="../experimental.html" title="c/experimental">技术规范</a> </td></tr>
</table></div><div></div></div></div></div><div class="t-navbar-sep"> </div><div class="t-navbar-head"><strong class="selflink">声明</strong><div class="t-navbar-menu"><div><div style="display:inline-block">
<div><table class="t-nv-begin" cellpadding="0" style="">
<tr class="t-nv"><td colspan="5"> <a href="pointer.html" title="c/language/pointer">指针</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="array.html" title="c/language/array">数组</a> </td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="enum.html" title="c/language/enum"><span class="t-lines"><span>enum</span></span></a></div></div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="struct.html" title="c/language/struct"><span class="t-lines"><span>struct</span></span></a></div></div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="union.html" title="c/language/union"><span class="t-lines"><span>union</span></span></a></div></div></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="bit_field.html" title="c/language/bit field">位域</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="atomic.html" title="c/language/atomic">原子类型</a> <span class="t-mark-rev t-since-c11">(C11)</span></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="const.html" title="c/language/const"><span class="t-lines"><span>const</span></span></a></div></div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="volatile.html" title="c/language/volatile"><span class="t-lines"><span>volatile</span></span></a></div></div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="restrict.html" title="c/language/restrict"><span class="t-lines"><span>restrict</span></span></a></div><div><span class="t-lines"><span><span class="t-mark-rev t-since-c99">(C99)</span></span></span></div></div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="_Alignas.html" title="c/language/ Alignas"><span class="t-lines"><span>_Alignas</span></span></a></div><div><span class="t-lines"><span><span class="t-mark-rev t-since-c11">(C11)</span></span></span></div></div></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="storage_duration.html" title="c/language/storage duration">存储期与链接</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="extern.html" title="c/language/extern">外部及试探性定义</a> </td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="typedef.html" title="c/language/typedef"><span class="t-lines"><span>typedef</span></span></a></div></div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="_Static_assert.html" title="c/language/ Static assert"><span class="t-lines"><span>_Static_assert</span></span></a></div><div><span class="t-lines"><span><span class="t-mark-rev t-since-c11">(C11)</span></span></span></div></div></td></tr>
</table></div>
</div><div></div></div></div></div><div class="t-navbar-sep"> </div></div>
<p><i>声明</i>是一个引入一个或多个<a href="identifier.html" title="c/language/identifier">标识符</a>到程序中，并指定其含义及属性的 C 语言构造。
</p><p>声明可以出现在任何作用域中。每个声明以分号结束（类似<a href="statements.html" title="c/language/statements">语句</a>），并由两个独立部分组成：
</p>
<table class="t-sdsc-begin">

<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr>
<tr class="t-sdsc">
<td class="t-sdsc-nopad"> <span class="t-spar">specifiers-and-qualifiers</span> <span class="t-spar">declarators-and-initializers</span> <code><b>;</b></code>
</td>
<td class="t-sdsc-nopad">
</td>
<td class="t-sdsc-nopad">
</td></tr>



<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr></table>
<p>其中
</p>
<table class="t-par-begin">


<tr class="t-par">
<td> <span class="t-spar">specifiers-and-qualifiers</span>
</td>
<td> -
</td>
<td>  任意顺序的下列内容的空白符分隔列表
<ul><li> 类型指定符：
</li></ul>
<dl><dd><ul><li> <span class="t-c"><span class="mw-geshi c source-c"><span class="kw4">void</span></span></span>
</li><li> <a href="arithmetic_types.html" title="c/language/arithmetic types">算术类型</a>名称
</li><li> <a href="atomic.html" title="c/language/atomic">原子类型</a>名称
</li><li> 先前由 <a href="typedef.html" title="c/language/typedef">typedef</a> 声明引入的名称
</li><li> <a href="struct.html" title="c/language/struct">struct</a> 、 <a href="union.html" title="c/language/union">union</a> 或 <a href="enum.html" title="c/language/enum">enum</a> 指定符
</li></ul>
</dd></dl>
<ul><li> 零或一个存储类指定符： <a href="typedef.html" title="c/language/typedef">typedef</a> 、 <a href="storage_duration.html" title="c/language/storage duration">auto 、 register 、 static 、 extern、 _Thread_local</a>
</li><li> 零或多个类型限定符： <a href="const.html" title="c/language/const">const</a> 、 <a href="volatile.html" title="c/language/volatile">volatile</a>、 <a href="restrict.html" title="c/language/restrict">restrict</a>、 <a href="atomic.html" title="c/language/atomic">_Atomic</a>
</li><li> （只在声明函数时）零或多个函数指定符： <a href="inline.html" title="c/language/inline">inline</a> 、 <a href="_Noreturn.html" title="c/language/ Noreturn">_Noreturn</a>
</li><li> 零或多个对齐指定符： <a href="_Alignas.html" title="c/language/ Alignas">_Alignas</a>
</li></ul>
</td></tr>
<tr class="t-par">
<td> <span class="t-spar">declarators-and-initializers</span>
</td>
<td> -
</td>
<td>  <span class="t-spar">declarators</span> 的逗号分隔列表（每个声明器提供附加类型信息及/或要声明的标识符）。 声明器可伴随<a href="initialization.html" title="c/language/initialization">初始化器</a>。 <a href="enum.html" title="c/language/enum">enum</a> 、 <a href="struct.html" title="c/language/struct">struct</a> 和 <a href="union.html" title="c/language/union">union</a> 声明可忽略 <span class="t-spar">declarators</span> ，这种情况下它们仅引入枚举常量和/或标签。
<p><br>
</p>
</td></tr></table>
<p>例如：
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="c source-c"><pre class="de1"><span class="kw4">int</span> a, <span class="sy2">*</span>b<span class="sy1">=</span><a href="../types/NULL.html"><span class="kw103">NULL</span></a><span class="sy4">;</span> <span class="co1">// “ int ”是类型指定符，</span>
                <span class="co1">// “ a ”是声明器</span>
                <span class="co1">// “ *b ”是声明器而 NULL 是初始化器</span>
<span class="kw4">const</span> <span class="kw4">int</span> <span class="sy2">*</span>f<span class="br0">(</span><span class="kw4">void</span><span class="br0">)</span><span class="sy4">;</span> <span class="co1">// “ int ”是类型指定符</span>
                    <span class="co1">// “ const ”是类型限定符</span>
                    <span class="co1">// “ *f(void) ”是声明器</span>
<span class="kw2">enum</span> COLOR <span class="br0">{</span>RED, GREEN, BLUE<span class="br0">}</span> c<span class="sy4">;</span> <span class="co1">// “ enum COLOR {RED, GREEN, BLUE} ”是类型指定符</span>
                                 <span class="co1">// “ c ”是声明器</span></pre></div></div>
<p>一个声明引入的每个标识符类型是通过 <span class="t-spar">type specifier</span> 所指定的类型及其 <span class="t-spar">declarator</span> 所应用的类型修饰决定的。
</p>
<h3><span class="mw-headline" id=".E5.A3.B0.E6.98.8E.E5.99.A8">声明器</span></h3>
<p>每个声明器是下列之一：
</p>
<table class="t-sdsc-begin">

<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr>
<tr class="t-sdsc">
<td> <span class="t-spar">identifier</span>
</td>
<td> (1)
</td>
<td class="t-sdsc-nopad">
</td></tr>



<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr>
<tr class="t-sdsc">
<td> <code><b>(</b></code> <span class="t-spar">declarator</span> <code><b>)</b></code>
</td>
<td> (2)
</td>
<td class="t-sdsc-nopad">
</td></tr>



<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr>
<tr class="t-sdsc">
<td> <code><b>*</b></code> <span class="t-spar">qualifiers</span><span class="t-mark">(可选)</span> <span class="t-spar">declarator</span>
</td>
<td> (3)
</td>
<td class="t-sdsc-nopad">
</td></tr>



<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr>
<tr class="t-sdsc">
<td> <span class="t-spar">noptr-declarator</span> <code><b>[</b></code> <span class="t-spar">static</span><span class="t-mark">(可选)</span> <span class="t-spar">qualifiers</span><span class="t-mark">(可选)</span> <span class="t-spar">expression</span> <code><b>]</b></code>
<p><span class="t-spar">noptr-declarator</span> <code><b>[</b></code> <span class="t-spar">qualifiers</span><span class="t-mark">(可选)</span> <code><b>*</b></code> <code><b>]</b></code>
</p>
</td>
<td> (4)
</td>
<td class="t-sdsc-nopad">
</td></tr>



<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr>
<tr class="t-sdsc">
<td> <span class="t-spar">noptr-declarator</span> <code><b>(</b></code> <span class="t-spar">parameters-or-identifiers</span> <code><b>)</b></code>
</td>
<td> (5)
</td>
<td class="t-sdsc-nopad">
</td></tr>



<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr></table>
<div class="t-li1"><span class="t-li">1)</span> 此声明器引入的标识符。</div>
<div class="t-li1"><span class="t-li">2)</span> 任何可以放入括号中的声明器；引入指向数组或指向函数指针时要求这么做。</div>
<div class="t-li1"><span class="t-li">3)</span> <a href="pointer.html" title="c/language/pointer">指针声明器</a>：声明 <span class="t-c"><span class="mw-geshi c source-c">S <span class="sy2">*</span> cvr D</span></span> 声明 <code>D</code> 为 <span class="t-spar">cvr</span> 限定指针，指向 <code>S</code> 所确定的类型。</div>
<div class="t-li1"><span class="t-li">4)</span> <a href="array.html" title="c/language/array">数组声明器</a>：声明 <span class="t-c"><span class="mw-geshi c source-c">S D<span class="br0">[</span>N<span class="br0">]</span></span></span> 声明 <code>D</code> 为有 <code>N</code> 个 <code>S</code> 所确定类型对象的数组。 <span class="t-spar">noptr-declarator</span> 是无括号指针声明器以外的其他任何声明器。</div>
<div class="t-li1"><span class="t-li">5)</span> <a href="function_declaration.html" title="c/language/function declaration">函数声明器</a>：声明 <span class="t-c"><span class="mw-geshi c source-c">S D<span class="br0">(</span>params<span class="br0">)</span></span></span> 声明 <code>D</code> 为接收参数 <code>params</code> 并返回 <code>S</code> 的函数。 <span class="t-spar">noptr-declarator</span> 是无括号指针声明器以外的其他任何声明器。</div>
<p>此语法背后的原因，是当声明器所声明的标识符以与声明器相同的形式出现在表达式中时，它会拥有类型指定符序列所指定的类型。
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="c source-c"><pre class="de1"><span class="kw1">struct</span> C <span class="br0">{</span>
    <span class="kw4">int</span> member<span class="sy4">;</span> <span class="co1">// “ int ”是类型指定符</span>
                <span class="co1">// “ member ”是声明器</span>
<span class="br0">}</span> obj, <span class="sy2">*</span>pObj <span class="sy1">=</span> <span class="sy3">&amp;</span>obj<span class="sy4">;</span>
<span class="co1">// “ struct C { int member; } ”是类型指定符</span>
<span class="co1">// 声明器“ obj ”定义 struct C 类型的对象</span>
<span class="co1">// 声明器“ *pObj ”声明指向 struct C 的指针，</span>
<span class="co1">// 初始化器“ = &amp;obj ”提供该指针的初值</span>
 
<span class="kw4">int</span> a <span class="sy1">=</span> <span class="nu0">1</span>, <span class="sy2">*</span>p <span class="sy1">=</span> <a href="../types/NULL.html"><span class="kw103">NULL</span></a>, f<span class="br0">(</span><span class="kw4">void</span><span class="br0">)</span>, <span class="br0">(</span><span class="sy2">*</span>pf<span class="br0">)</span><span class="br0">(</span><span class="kw4">double</span><span class="br0">)</span><span class="sy4">;</span>
<span class="co1">// 类型指定符是“ int ”</span>
<span class="co1">// 声明器“ a ”定义一个 int 类型对象</span>
<span class="co1">//   初始化器“=1”提供其初值</span>
<span class="co1">// 声明器“ *p ”定义一个指向 int 指针类型的对象</span>
<span class="co1">//   初始化器“ =NULL ”提供其初值</span>
<span class="co1">// 声明器“ f(void) ”声明接受 void 并返回 int 的函数</span>
<span class="co1">// 声明器“ (*pf)(double) ”定义一个指向</span>
<span class="co1">//   接受 double 并返回 int 的函数的指针类型对象</span>
 
<span class="kw4">int</span> <span class="br0">(</span><span class="sy2">*</span><span class="br0">(</span><span class="sy2">*</span>foo<span class="br0">)</span><span class="br0">(</span><span class="kw4">double</span><span class="br0">)</span><span class="br0">)</span><span class="br0">[</span><span class="nu0">3</span><span class="br0">]</span> <span class="sy1">=</span> <a href="../types/NULL.html"><span class="kw103">NULL</span></a><span class="sy4">;</span>
<span class="co1">// 类型指定符是“int”</span>
<span class="co1">// 1. 声明器“ (*(*foo)(double))[3] ”是数组声明器：</span>
<span class="co1">//    所声明类型是“ 3 个 int 的数组的 /嵌套声明器/ ”</span>
<span class="co1">// 2. 嵌套声明器是“ *(*foo)(double)) ”，是指针声明器</span>
<span class="co1">//    所声明类型是“ /嵌套声明器/ 指向 3 个 int 的数组的指针”</span>
<span class="co1">// 3. 嵌套声明器是“ (*foo)(double) ”，是一个函数声明器</span>
<span class="co1">//    所声明类型是“ /嵌套声明器/ 接受 double 并返回指向 3 个 int 的数组的指针的函数”</span>
<span class="co1">// 4. 嵌套声明器是“ (*foo) ”，是一个（有括号，函数声明器所要求）指针声明器。</span>
<span class="co1">//    所声明类型是“ /嵌套声明器/ 指向接受 double 并返回指向 3 个 int 的数组的指针的函数的指针”</span>
<span class="co1">// 5. 嵌套声明器是“ foo ”，是一个标识符。</span>
<span class="co1">// 该声明引入一个标识符“ foo ”，以指代一个对象，其类型为</span>
<span class="co1">// “指向接受 double 并返回指向 3 个 int 的数组的指针的函数的指针”</span>
<span class="co1">// 初始化器“ = NULL ”提供此指针的初值。</span>
 
<span class="co1">// 若在用于声明符形式的表达式使用“foo”，则表达式类型将是int。</span>
<span class="kw4">int</span> x <span class="sy1">=</span> <span class="br0">(</span><span class="sy2">*</span><span class="br0">(</span><span class="sy2">*</span>foo<span class="br0">)</span><span class="br0">(</span><span class="nu16">1.2</span><span class="br0">)</span><span class="br0">)</span><span class="br0">[</span><span class="nu0">0</span><span class="br0">]</span><span class="sy4">;</span></pre></div></div>
<p>每个不属于其他声明器一部分的声明器结尾是一个<a href="eval_order.html" title="c/language/eval order">顺序点</a>。
</p>
<h3><span class="mw-headline" id=".E5.AE.9A.E4.B9.89">定义</span></h3>
<p><i>定义</i>是一个提供所有关于其所声明标识符信息的声明。
</p><p>每个 <a href="enum.html" title="c/language/enum">enum</a> 或 <a href="typedef.html" title="c/language/typedef">typedef</a> 声明都是定义。
</p><p>对于函数，包含函数体的声明即是<a href="function_definition.html" title="c/language/function definition">函数定义</a>：
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="c source-c"><pre class="de1"><span class="kw4">int</span> foo<span class="br0">(</span><span class="kw4">double</span><span class="br0">)</span><span class="sy4">;</span> <span class="co1">// 声明</span>
<span class="kw4">int</span> foo<span class="br0">(</span><span class="kw4">double</span> x<span class="br0">)</span><span class="br0">{</span> <span class="kw1">return</span> x<span class="sy4">;</span> <span class="br0">}</span> <span class="co1">// 定义</span></pre></div></div>
<p>对于对象，分配其存储的声明（<a href="storage_duration.html" title="c/language/storage duration">自动或静态</a>，但、非 extern ）即是定义，而一个不分配存储的声明（<a href="extern.html" title="c/language/extern">外部声明</a>）不是。
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="c source-c"><pre class="de1"><span class="kw2">extern</span> <span class="kw4">int</span> n<span class="sy4">;</span> <span class="co1">// 声明</span>
<span class="kw4">int</span> n <span class="sy1">=</span> <span class="nu0">10</span><span class="sy4">;</span> <span class="co1">// 定义</span></pre></div></div>
<p>对于<a href="struct.html" title="c/language/struct">结构体</a>和<a href="union.html" title="c/language/union">联合体</a>，指定其成员列表的声明是定义：
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="c source-c"><pre class="de1"><span class="kw1">struct</span> X<span class="sy4">;</span> <span class="co1">// 声明</span>
<span class="kw1">struct</span> X <span class="br0">{</span> <span class="kw4">int</span> n<span class="sy4">;</span> <span class="br0">}</span><span class="sy4">;</span> <span class="co1">// 定义</span></pre></div></div>
<h3><span class="mw-headline" id=".E9.87.8D.E5.A3.B0.E6.98.8E">重声明</span></h3>
<p>若另一个同一标识符的声明在同一<a href="scope.html" title="c/language/scope">作用域</a>的较早部分出现，则声明不可再引入同一标识符，除非
</p>
<ul><li> <a href="storage_duration.html" title="c/language/storage duration">有链接</a>对象（外部或内部）的声明可以重复：
</li></ul>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="c source-c"><pre class="de1"><span class="kw2">extern</span> <span class="kw4">int</span> x<span class="sy4">;</span>
<span class="kw4">int</span> x <span class="sy1">=</span> <span class="nu0">10</span><span class="sy4">;</span> <span class="co1">// OK</span>
<span class="kw2">extern</span> <span class="kw4">int</span> x<span class="sy4">;</span> <span class="co1">// OK</span>
 
<span class="kw4">static</span> <span class="kw4">int</span> n<span class="sy4">;</span>
<span class="kw4">static</span> <span class="kw4">int</span> n <span class="sy1">=</span> <span class="nu0">10</span><span class="sy4">;</span> <span class="co1">// OK</span>
<span class="kw4">static</span> <span class="kw4">int</span> n<span class="sy4">;</span> <span class="co1">// OK</span></pre></div></div>
<ul><li> 非 VLA <a href="typedef.html" title="c/language/typedef">typedef</a> 可以任意重复，只要它命名同一类型：
</li></ul>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="c source-c"><pre class="de1"><span class="kw1">typedef</span> <span class="kw4">int</span> int_t<span class="sy4">;</span> 
<span class="kw1">typedef</span> <span class="kw4">int</span> int_t<span class="sy4">;</span> <span class="co1">// OK</span></pre></div></div>
<ul><li> <a href="struct.html" title="c/language/struct">struct</a> 和 <a href="union.html" title="c/language/union">union</a> 声明可以重复：
</li></ul>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="c source-c"><pre class="de1"><span class="kw1">struct</span> X<span class="sy4">;</span>
<span class="kw1">struct</span> X <span class="br0">{</span> <span class="kw4">int</span> n<span class="sy4">;</span> <span class="br0">}</span><span class="sy4">;</span>
<span class="kw1">struct</span> X<span class="sy4">;</span></pre></div></div>
<p>这些规则会简化头文件的使用。
</p>
<h3><span class="mw-headline" id=".E6.B3.A8.E6.84.8F">注意</span></h3>
 <table class="t-rev-begin">
<tr class="t-rev t-until-c99"><td>
<p>C89 中，任何<a href="statements.html#.E5.A4.8D.E5.90.88.E8.AF.AD.E5.8F.A5" title="c/language/statements">复合语句</a>（块作用域）中的声明必须出现在块的开头，在任何<a href="statements.html" title="c/language/statements">语句</a>之前。
</p><p>而且，C89 中返回 <span class="t-c"><span class="mw-geshi c source-c"><span class="kw4">int</span></span></span> 的函数可以隐式地用<a href="operator_other.html#.E5.87.BD.E6.95.B0.E8.B0.83.E7.94.A8" title="c/language/operator other">函数调用运算符</a>声明，且使用旧式<a href="function_definition.html" title="c/language/function definition">函数定义</a>时， <span class="t-c"><span class="mw-geshi c source-c"><span class="kw4">int</span></span></span> 类型的函数参数不必声明。
</p>
</td>
<td><span class="t-mark-rev t-until-c99">(C99 前)</span></td></tr>
</table>
<p>禁止空声明器；声明必须<span class="t-rev-inl t-since-c11"><span>是一个 <a href="_Static_assert.html" title="c/language/ Static assert">_Static_assert</a> 声明或</span> <span><span class="t-mark-rev t-since-c11">(C11 起)</span></span></span>拥有至少一个声明器，或声明至少一个 struct/union/enum 标签，或引入至少一个枚举常量。
</p>
 <table class="t-rev-begin">
<tr class="t-rev t-since-c99"><td>
<p>若声明器的任一部分是 <a href="array.html" title="c/language/array">VLA 数组</a>声明器，则整个声明器的类型被称作“可变修改类型”。根据可变修改类型定义的类型同样是可变修改的（ VM ）。
</p><p>任何可变修改类型声明只允许出现在<a href="scope.html" title="c/language/scope">块作用域</a>或函数原型作用域，而且不能是任何结构体或联合体的成员。尽管 <a href="array.html" title="c/language/array">VLA</a> 只能拥有自动<a href="storage_duration.html" title="c/language/storage duration">存储期</a>，一个 VM 类型，例如指向 VLA 的指针也可以有静态存储。关于 VM 类型有其他使用限制，见 <a href="goto.html" title="c/language/goto">goto</a> 、 <a href="switch.html" title="c/language/switch">switch</a> 、 <span class="t-lc"><a href="../program/longjmp.html" title="c/program/longjmp">longjmp</a></span> 。
</p>
</td>
<td><span class="t-mark-rev t-since-c99">(C99 起)</span></td></tr>
</table>
 <table class="t-rev-begin">
<tr class="t-rev t-since-c11"><td>
<p><a href="_Static_assert.html" title="c/language/ Static assert">_Static_assert</a> 从 C 文法的视角来看，被认为是声明（故它们可以出现在任何声明能出现的地方），但它们不会引入任何新的标识符，且不遵循声明语法。
</p>
</td>
<td><span class="t-mark-rev t-since-c11">(C11 起)</span></td></tr>
</table>
<h3><span class="mw-headline" id=".E5.BC.95.E7.94.A8">引用</span></h3>
<div class="t-ref-std-11">
<ul><li> C11 standard (ISO/IEC 9899:2011): 
</li></ul>
<dl><dd><ul><li> 6.7 Declarations (p: 108-145)
</li></ul>
</dd></dl>
<div class="t-ref-std-c99">
<ul><li> C99 standard (ISO/IEC 9899:1999): 
</li></ul>
<dl><dd><ul><li> 6.7 Declarations (p: 97-130)
</li></ul>
</dd></dl>
<div class="t-ref-std-c89">
<ul><li> C89/C90 standard (ISO/IEC 9899:1990): 
</li></ul>
<dl><dd><ul><li> 3.5 Declarations 
</li></ul>
</dd></dl>
</div>
<h3><span class="mw-headline" id=".E5.8F.82.E9.98.85">参阅</span></h3>
<table class="t-dsc-begin">

</table></div>
</div>

<!-- 
NewPP limit report
Preprocessor visited node count: 3163/1000000
Preprocessor generated node count: 7577/1000000
Post‐expand include size: 57265/2097152 bytes
Template argument size: 16351/2097152 bytes
Highest expansion depth: 16/40
Expensive parser function count: 0/100
-->

<!-- Saved in parser cache with key mwiki1-mwiki_zh_:pcache:idhash:7943-0!*!0!!zh!*!zh!* and timestamp 20200216155651 -->
</div>                    <!-- /bodycontent -->
                                        <!-- printfooter -->
                    <div class="printfooter">
                    来自“<a href="https://zh.cppreference.com/mwiki/index.php?title=c/language/declarations&amp;oldid=56726">https://zh.cppreference.com/mwiki/index.php?title=c/language/declarations&amp;oldid=56726</a>”                    </div>
                    <!-- /printfooter -->
                                                            <!-- catlinks -->
                    <!-- /catlinks -->
                                                            <div class="visualClear"></div>
                    <!-- debughtml -->
                                        <!-- /debughtml -->
                </div>
                <!-- /bodyContent -->
            </div>
        </div>
        <!-- /content -->
        <!-- footer -->
        <div id="cpp-footer-base" class="noprint">
            <div id="footer">
                        <div id="cpp-navigation">
            <h5>导航</h5>
            <ul><li><a href="https://zh.cppreference.com/w/c/language/declarations">Online version</a></li><li>Offline version retrieved 2020-03-14 18:00.</li></ul></div>
                        <ul id="footer-info">
                                    <li id="footer-info-lastmod"> 本页面最后修改于2018年6月29日 (星期五) 18:45。</li>
                                    <li id="footer-info-viewcount">此页面已被浏览过6,519次。</li>
                            </ul>
                    </div>
        </div>
        <!-- /footer -->
        <script>if(window.mw){
mw.loader.state({"site":"loading","user":"missing","user.groups":"ready"});
}</script>
<script src="../../../common/skin_scripts.js"></script>
<script>if(window.mw){
mw.loader.load(["mediawiki.action.view.postEdit","mediawiki.user","mediawiki.page.ready","mediawiki.searchSuggest","mediawiki.hidpi","ext.gadget.MathJax","ext.gadget.ColiruCompiler"], null, true);
}</script>
<script src="../../../common/site_scripts.js"></script>
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-2828341-1']);
_gaq.push(['_setDomainName', 'cppreference.com']);
_gaq.push(['_trackPageview']);
</script><!-- Served in 0.884 secs. -->
	</body>
<!-- Cached 20200216155651 -->
</html>